package com.jxcq.tensquare.user.controller;
import java.util.HashMap;
import java.util.Map;

import com.jxcq.tensquare.user.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.jxcq.tensquare.user.service.UserService;

import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import util.JwtUtil;

/**
 * 控制器层
 * @author CalvinXCui
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private JwtUtil jwtUtil;

	/**
	 * 更新好友粉丝数和用户关注数
	 */
	@PutMapping("{userid}/{friendid}/{x}")
	public void updatefasnscountfollowcount(@PathVariable String userid,@PathVariable String friendid,@PathVariable int x){
		userService.updatefasnscountfollowcount(x,userid,friendid);
	}

	@PostMapping("login")
	public Result login(@RequestBody Map<String,String> loginMap){

		User user = userService.findByMobileAndPassword(loginMap.get("mobile"),loginMap.get("password"));
		if(user!=null){
			String token = jwtUtil.createJWT(user.getId(), user.getMobile(), "user");
			Map<String,String> map = new HashMap<>(5);
			map.put("token",token);
			map.put("role","user");
			return new Result(true,StatusCode.SUCCESS,"登陆成功");
		}else{
			return new Result(false,StatusCode.LOGIN_ERROR,"用户名或密码错误");
		}
	}


	@PostMapping("register/{code}")
	public Result regist(@PathVariable String code,@RequestBody User user){
        String mobile = user.getMobile();
        String verifCodeRedis = (String)redisTemplate.opsForValue().get("verifCode_"+mobile);
		if(verifCodeRedis.isEmpty()){
			return new Result(false,StatusCode.FAILE,"请获取验证码");
		}
		if(!StringUtils.equals(verifCodeRedis,code)){
			return new Result(false,StatusCode.FAILE,"验证码错误");
		}
		User userMobile = userService.findByMobile(mobile);
		if(userMobile != null){
			return new Result(false,StatusCode.REPEAT_ERROR,"该用户已注册");
		}
		userService.add(user);
		return new Result(true,StatusCode.SUCCESS,"注册成功");

	}

	@PostMapping("sendsms/{mobile}")
	public Result  sendVerificationCode(@PathVariable String mobile){
		userService.sendVerificationCode(mobile);
        return new Result(true,StatusCode.SUCCESS,"发送成功");
	}
	
	
	/**
	 * 查询全部数据
	 * @return
	 */
	@GetMapping
	public Result findAll(){
		return new Result(true,StatusCode.SUCCESS,"查询成功",userService.findAll());
	}
	
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@GetMapping(value="{id}")
	public Result findById(@PathVariable String id){
		return new Result(true,StatusCode.SUCCESS,"查询成功",userService.findById(id));
	}


	/**
	 * 分页+多条件查询
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
		Page<User> pageList = userService.findSearch(searchMap, page, size);
		return  new Result(true,StatusCode.SUCCESS,"查询成功",  new PageResult<User>(pageList.getTotalElements(), pageList.getContent()) );
	}

	/**
     * 根据条件查询
     * @param searchMap
     * @return
     */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch( @RequestBody Map searchMap){
        return new Result(true,StatusCode.SUCCESS,"查询成功",userService.findSearch(searchMap));
    }
	
	/**
	 * 增加
	 * @param user
	 */
	@RequestMapping(method=RequestMethod.POST)
	public Result add(@RequestBody User user  ){
		userService.add(user);
		return new Result(true,StatusCode.SUCCESS,"增加成功");
	}
	
	/**
	 * 修改
	 * @param user
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.PUT)
	public Result update(@RequestBody User user, @PathVariable String id ){
		user.setId(id);
		userService.update(user);		
		return new Result(true,StatusCode.SUCCESS,"修改成功");
	}
	
	/**
	 * 只有管理员才可以删除普通用户
	 * 删除
	 * @param id
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result delete(@PathVariable String id ){
		userService.deleteById(id);
		return new Result(true,StatusCode.SUCCESS,"删除成功");
	}
	
}
